<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- creator-debugger-example.qdoc -->
  <title>Debugging a C++ Example Application | Qt Creator Manual</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt Creator Manual</a></td><td >Debugging a C++ Example Application</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="index.html">Qt Creator Manual 4.11.1</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="creator-debugging-qml.html" />
  <link rel="next" href="creator-qml-debugging-example.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="creator-debugging-qml.html">Debugging Qt Quick Projects</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="creator-qml-debugging-example.html">Debugging a Qt Quick Example Application</a>
</p><p/>
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Debugging a C++ Example Application</h1>
<span class="subtitle"></span>
<!-- $$$creator-debugging-example.html-description -->
<div class="descr"> <a name="details"></a>
<p>This section uses the <a href="creator-writing-program.html">TextFinder</a> example to illustrate how to debug Qt C++ applications in the <b>Debug</b> mode.</p>
<p>TextFinder reads a text file into <a href="http://doc.qt.io/qt-5/qstring.html">QString</a> and then displays it with <a href="http://doc.qt.io/qt-5/qtextedit.html">QTextEdit</a>. To look at the TextFinder class and see the stored data, place a breakpoint in textfinder.cpp, as follows:</p>
<ol class="1" type="1"><li>Click in between the line number and the window border on the line where we change the cursor position to set a breakpoint.<p class="centerAlign"><img src="images/qtcreator-setting-breakpoint1.png" alt="" /></p></li>
<li>Select <b>Debug</b> &gt; <b>Start Debugging</b> &gt; <b>Start Debugging of Startup Project</b> or press <b>F5</b>.</li>
<li>To view information about the breakpoint, go to the <b>Breakpoints</b> view.<p class="centerAlign"><img src="images/qtcreator-setting-breakpoint2.png" alt="" /></p></li>
<li>To remove a breakpoint, right-click it and select <b>Delete Breakpoint</b>.</li>
<li>To view the base classes and data members of the TextFinder class, go to the <b>Locals</b> view.<p class="centerAlign"><img src="images/qtcreator-watcher.png" alt="" /></p></li>
</ol>
<p>Modify the <code>on_findButton_clicked()</code> function to move back to the start of the document and continue searching once the cursor hits the end of the document. Add the following code snippet:</p>
<pre class="cpp">

  <span class="type">void</span> TextFinder<span class="operator">::</span>on_findButton_clicked()
  {
      <span class="type"><a href="http://doc.qt.io/qt-5/qstring.html">QString</a></span> searchString <span class="operator">=</span> ui<span class="operator">-</span><span class="operator">&gt;</span>lineEdit<span class="operator">-</span><span class="operator">&gt;</span>text();

      <span class="type"><a href="http://doc.qt.io/qt-5/qtextdocument.html">QTextDocument</a></span> <span class="operator">*</span>document <span class="operator">=</span> ui<span class="operator">-</span><span class="operator">&gt;</span>textEdit<span class="operator">-</span><span class="operator">&gt;</span>document();
      <span class="type"><a href="http://doc.qt.io/qt-5/qtextcursor.html">QTextCursor</a></span> cursor <span class="operator">=</span> ui<span class="operator">-</span><span class="operator">&gt;</span>textEdit<span class="operator">-</span><span class="operator">&gt;</span>textCursor();
      cursor <span class="operator">=</span> document<span class="operator">-</span><span class="operator">&gt;</span>find(searchString<span class="operator">,</span> cursor<span class="operator">,</span>
          <span class="type"><a href="http://doc.qt.io/qt-5/qtextdocument.html">QTextDocument</a></span><span class="operator">::</span>FindWholeWords);
      ui<span class="operator">-</span><span class="operator">&gt;</span>textEdit<span class="operator">-</span><span class="operator">&gt;</span>setTextCursor(cursor);

      bool found <span class="operator">=</span> cursor<span class="operator">.</span>isNull();

      <span class="keyword">if</span> (<span class="operator">!</span>found <span class="operator">&amp;</span><span class="operator">&amp;</span> previouslyFound) {
          <span class="type">int</span> ret <span class="operator">=</span> <span class="type"><a href="http://doc.qt.io/qt-5/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>question(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;End of Document&quot;</span>)<span class="operator">,</span>
          tr(<span class="string">&quot;I have reached the end of the document. Would you like &quot;</span>
          <span class="string">&quot;me to start searching from the beginning of the document?&quot;</span>)<span class="operator">,</span>
          <span class="type"><a href="http://doc.qt.io/qt-5/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Yes <span class="operator">|</span> <span class="type"><a href="http://doc.qt.io/qt-5/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>No<span class="operator">,</span> <span class="type"><a href="http://doc.qt.io/qt-5/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Yes);

          <span class="keyword">if</span> (ret <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="http://doc.qt.io/qt-5/qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>Yes) {
              cursor <span class="operator">=</span> document<span class="operator">-</span><span class="operator">&gt;</span>find(searchString<span class="operator">,</span>
                  <span class="type"><a href="http://doc.qt.io/qt-5/qtextdocument.html">QTextDocument</a></span><span class="operator">::</span>FindWholeWords);
              ui<span class="operator">-</span><span class="operator">&gt;</span>textEdit<span class="operator">-</span><span class="operator">&gt;</span>setTextCursor(cursor);
          } <span class="keyword">else</span>
              <span class="keyword">return</span>;
      }
      previouslyFound <span class="operator">=</span> found;
  }

</pre>
<p>If you compile and run the above code, however, the application does not work correctly due to a logic error. To locate this logic error, step through the code using the following buttons: <img src="images/qtcreator-debug-button-stop.png" alt="" /> (<b>Stop Debugger</b>), <img src="images/debugger_stepover_small.png" alt="" /> (<b>Step Over</b>), <img src="images/debugger_stepinto_small.png" alt="" /> (<b>Step Into</b>), and <img src="images/debugger_stepout_small.png" alt="" /> (<b>Step Out</b>).</p>
</div>
<!-- @@@creator-debugging-example.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="creator-debugging-qml.html">Debugging Qt Quick Projects</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="creator-qml-debugging-example.html">Debugging a Qt Quick Example Application</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
